package com.uenpay.im.ui.old.activity.label;

import com.qyx.android.entity.FriendEntity;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
/**
 * @author SL
 * @created 2015-12-28 12:48:59
 * @copyRight http://www.qiyunxin.com
 * @function 获取标签
 */
public class GetLabelsInvokeItem extends BaseHttpInvokeItem {
	public class GetLabelsInvokeItemResult extends HttpInvokeResult {
		public ArrayList<LabelEntity> arrayList = new ArrayList<LabelEntity>();
	}

	public GetLabelsInvokeItemResult getOutput() {
		return (GetLabelsInvokeItemResult) GetResultObject();
	}

	public GetLabelsInvokeItem() {
		String url = APIConfiguration.getBaseUrl() + "Friend/Friend/getTags?"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetLabelsInvokeItemResult result = new GetLabelsInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		JSONArray jsonArray = json.optJSONArray("data");
		if (jsonArray != null) {
			for (int i = 0, size = jsonArray.length(); i < size; i++) {
				result.arrayList
						.add(desLabelEntity(jsonArray.optJSONObject(i)));
			}
		}
		return result;
	}

	private LabelEntity desLabelEntity(JSONObject json) {
		LabelEntity labelEntity = new LabelEntity();
		labelEntity.labelId = json.optInt("tagid");
		labelEntity.labelName = json.optString("tagname");
		JSONArray jsonArray = json.optJSONArray("custs");
		if (jsonArray != null) {
			for (int i = 0, size = jsonArray.length(); i < size; i++) {
				labelEntity.arrayList.add(desFriendEntitys(jsonArray
						.optJSONObject(i)));
			}
		}
		return labelEntity;
	}

	private FriendEntity desFriendEntitys(JSONObject json) {
		FriendEntity friendEntity = new FriendEntity();
		friendEntity.cust_id = json.optString("custid");
		friendEntity.nick_name = json.optString("nickname");
		return friendEntity;
	}

}
